package com.ychong.library.view

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.util.TypedValue
import android.view.MotionEvent
import android.view.View
import com.ychong.library.R
import com.ychong.library.utils.ResUtils
import com.ychong.library.utils.ScreenUtils

class TianZiGeView : View {
    private var mTextColor: Int
    private var textThickness: Int
    private var crossColor: Int
    private var crossWidth: Int
    private var aroundColor: Int
    private var aroundWidth: Int
    private var aroundPaint: Paint? = null
    private var crossPaint:Paint? = null
    private var crossPath: Path? = null
    private var textPaint: Paint? = null
    private var textPath: Path? = null

    constructor(context: Context) : this(context, null)
    constructor(context: Context, attributeSet: AttributeSet?) : this(context, attributeSet, 0)

    @SuppressLint("Recycle")
    constructor(context: Context, attributeSet: AttributeSet?, defStyle: Int) : super(
        context,
        attributeSet,
        defStyle
    ) {
        val style = context.obtainStyledAttributes(
            attributeSet,
            R.styleable.TianZiGeView,
            defStyle,
            0
        )
        aroundWidth = style.getDimensionPixelSize(
            R.styleable.TianZiGeView_tzgAroundWidth, TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP, 5f, resources.displayMetrics
            ).toInt()
        )
        aroundColor = style.getColor(
            R.styleable.TianZiGeView_tzgAroundColor,
            ResUtils.getColor(context, R.color.color_0D71DF)
        )
        crossWidth = style.getDimensionPixelSize(
            R.styleable.TianZiGeView_tzgCrossWidth,
            TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 5f, resources.displayMetrics)
                .toInt()
        )
         crossColor = style.getColor(
             R.styleable.TianZiGeView_tzgCrossColor,
            ResUtils.getColor(context, R.color.color_0D71DF)
        )
        textThickness = style.getDimensionPixelSize(
            R.styleable.TianZiGeView_tzgTextThickness,
            TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 5f, resources.displayMetrics)
                .toInt()
        )
        mTextColor = style.getColor(
            R.styleable.TianZiGeView_tzgTextColor,
            ResUtils.getColor(context, R.color.color_333333)
        )
        init()
    }

    private fun init() {
        aroundPaint = Paint().apply {
            isAntiAlias = true
            color = aroundColor
            style = Paint.Style.STROKE
            strokeWidth = aroundWidth.toFloat()
        }
        crossPaint = Paint().apply {
            isAntiAlias = true
            color = crossColor
            style = Paint.Style.STROKE
            strokeWidth = crossWidth.toFloat()
        }
        crossPath = Path()
        textPaint = Paint().apply {
            isAntiAlias = true
            style = Paint.Style.STROKE
            strokeWidth = textThickness.toFloat()
            color = mTextColor
            //设置折线连接处
            strokeJoin = Paint.Join.ROUND
            strokeCap = Paint.Cap.ROUND
        }
        textPath = Path()

    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        val widthMode = MeasureSpec.getMode(widthMeasureSpec)
        var width = MeasureSpec.getSize(widthMeasureSpec)
        if (widthMode != MeasureSpec.EXACTLY) {
            //wrap_content
            width = ScreenUtils.dpToPx(200) + paddingLeft + paddingRight
        }

        val heightMode = MeasureSpec.getMode(heightMeasureSpec)
        var height = MeasureSpec.getSize(heightMeasureSpec)
        if (heightMode != MeasureSpec.EXACTLY) {
            //wrap_content
            height = ScreenUtils.dpToPx(200) + paddingTop + paddingBottom
        }
        setMeasuredDimension(width, height)
    }

    @SuppressLint("DrawAllocation")
    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        drawBg(canvas)
        drawText(canvas)

    }

    private fun drawBg(canvas: Canvas?) {
        //绘制四周实体线
        aroundPaint!!.pathEffect = null
        canvas!!.drawRect(
            aroundWidth / 2f,
            aroundWidth / 2f,
            width * 1f - aroundWidth / 2f,
            height * 1f - aroundWidth / 2f,
            aroundPaint!!
        )

        //绘制中间交叉虚线
        val floatArr: FloatArray = floatArrayOf(50f, 10f)
        crossPaint!!.pathEffect = DashPathEffect(floatArr, 0f)
        crossPath!!.moveTo(0f, height / 2f)
        crossPath!!.lineTo(width * 1f, height / 2f)
        crossPath!!.moveTo(width / 2f, 0f)
        crossPath!!.lineTo(width / 2f, height * 1f)
        canvas.drawPath(
            crossPath!!,
            crossPaint!!
        )
    }

    private fun drawText(canvas: Canvas?) {
        canvas!!.drawPath(textPath!!, textPaint!!)
    }
    private var preX:Float = 0.0f
    private var preY:Float = 0.0f

    @SuppressLint("ClickableViewAccessibility")
    override fun onTouchEvent(event: MotionEvent?): Boolean {
        val x = event!!.x
        val y = event.y
        if (x > aroundWidth + textThickness / 2
            && x < width - aroundWidth - textThickness / 2
            && y > aroundWidth + textThickness / 2
            && y < height - aroundWidth - textThickness / 2
        ) {
            when (event.action) {
                MotionEvent.ACTION_DOWN -> {
                    preX = x
                    preY = y
                }
                MotionEvent.ACTION_MOVE -> {
                    textPath!!.moveTo(preX, preY)
                    textPath!!.lineTo(x, y)
                    preX = x
                    preY = y
                    invalidate()
                }
                MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> {
                }
            }
        }
        return true
    }

    fun clear() {
        textPath!!.reset()
        requestLayout()
        invalidate()
    }
}